package com.woniuxy.carlife.carlifecomprehensiveweb.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecomprehensiveweb.domain.Rescue;
import com.woniuxy.carlife.carlifecomprehensiveweb.dto.RescueDto;
import com.woniuxy.carlife.carlifecomprehensiveweb.param.RescueParam;
import com.woniuxy.carlife.carlifecomprehensiveweb.service.RescueService;
import com.woniuxy.carlife.carlifecomprehensiveweb.util.MinioTemplate;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * @ClassName: RescueController
 *@Description: 道路救援前端控制器
 * @author: yanzhongliang     
 * @date: 2021-04-05
 */

@RestController
@Slf4j
@Api(tags = "门店救援接口信息")//用于描述接口类的相关信息，作用于类上
@RequestMapping("/rescue")
public class RescueController {
    //    引入文件上传配置
    @Resource
    private MinioTemplate mt;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private RescueService rescueService;

    /**
     * 功能描述〈救援现场图片上传〉
     * @author: yanzhongliang
     * @param: MultipartFile
     * @return: rescueImage
     * @Date 2021/4/12 21:51
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "救援现场图片上传",notes = "<span style='color:red;'>用来上传救援现场图片的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 70034,message = "上传成功"),
            @ApiResponse(code=70020,message = "上传门店图片为空")
    })

    @PostMapping("/rescueimage")
    public Result setStoreImage(@ModelAttribute MultipartFile storeRescueImage) {

        log.info("传入门店图片：参数：{}",storeRescueImage);
        //判断门店图片是否为空
        if (storeRescueImage!=null){
            try {
                LocalDate ld = LocalDate.now();
                //    设置上传文件名
                StringBuffer fileName = new StringBuffer("/");
                fileName.append(ld.getYear()).append("/")
                        .append(ld.getMonthValue()).append("/")
                        .append(ld.getDayOfMonth()).append("/")
                        .append(System.currentTimeMillis());
                String StoreInfoImage = storeRescueImage.getOriginalFilename().substring(storeRescueImage.getOriginalFilename().lastIndexOf("."));
                fileName.append(StoreInfoImage);
                //        前端上传门店图片后转化的url
                String rescueImage = mt.uploadFile(fileName.toString(), "rescue",
                        storeRescueImage.getInputStream(),
                        storeRescueImage.getSize(),
                        storeRescueImage.getContentType());

                log.info("得到救援现场图片上传后返回的地址：参数：{}",rescueImage);
                //将上传云服务器后返回的地址返回前端
                return new Result(true, StatusCode.IMAGE_SUCCESS, "上传门店图片成功",rescueImage);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("上传失败");

            }
        }else {
            return new Result(false, StatusCode.STORE_IMAGE_EMPTY, "上传门店图片为空，请重新上传");
        }
    }

    /**
     * 功能描述〈添加门店救援信息记录〉
     * @author: yanzhongliang
     * @param: RescueParam
     * @return: message
     * @Date 2021/4/15 14:35
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "救援记录新增上传",notes = "<span style='color:red;'>用来上传救援记录新增的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code =40013,message = "救援信息记录新增成功"),
            @ApiResponse(code=40014,message = "救援信息记录新增失败"),
            @ApiResponse(code=70042,message = "输入被救援车型为空"),
            @ApiResponse(code=70031,message = "输入救援车主名为空"),
            @ApiResponse(code=70041,message = "输入救援时间为空"),
            @ApiResponse(code=70028,message = "输入救援门店电话为空"),
            @ApiResponse(code=70030,message = "输入救援门店名为空"),
            @ApiResponse(code=70044,message = "输入救援收费为空"),
            @ApiResponse(code=70026,message = "输入救援地址精度为空"),
            @ApiResponse(code=70028,message = "输入被救援车主电话为空"),
            @ApiResponse(code=70043,message = "输入被救援车主车牌号为空"),
            @ApiResponse(code=70027,message = "输入救援地址纬度为空"),
            @ApiResponse(code=70020,message = "输入救援现场图片为空"),
            @ApiResponse(code=70032,message = "输入救援地址为空"),
    })

    @PostMapping("/RescueInfoList")
    public Result setRescueInfoList(@ModelAttribute  @ApiParam RescueParam rescueParam){

        log.info("接收到传入救援信息记录：参数：{}",rescueParam);
        //判断传入救援地址是否为空
        if (rescueParam.getRescueAddress()!=null){
            //判断传入救援现场图片是否为空
            if (rescueParam.getRescueImage()!=null){
                //判断传入救援地址纬度是否为空
                if (rescueParam.getRescueLatitude()!=null) {
                    //判断传入被救援车主车牌号是否为空
                    if (rescueParam.getRescueLicense()!=null) {
                        //判断传入被救援车主电话是否为空
                        if (rescueParam.getRescuePhone()!=null) {
                            //判断传入救援地址精度是否为空
                            if (rescueParam.getRescuePrecision()!=null) {
                                //判断传入救援收费是否为空
                                if (rescueParam.getRescuePrice()!=null) {
                                    //判断传入救援门店名是否为空
                                    if (rescueParam.getRescueStore()!=null) {
                                        //判断传入救援门店电话是否为空
                                        if (rescueParam.getRescueStorePhone()!=null){
                                            //判断传入救援时间是否为空
                                            if (rescueParam.getRescueTime()!=null) {
                                                //判断传入被救援车主名是否为空
                                                if (rescueParam.getRescueUserName()!=null) {
                                                    //判断传入被救援车型是否为空
                                                    if (rescueParam.getRescueVehicle()!=null) {

                                                        log.info("开始执行新增救援记录");
                                                        //执行新增操作
                                                        int codss=rescueService.setRsecueInfolist(rescueParam);

                                                        log.info("执行完毕救援记录新增操作(返回 0 ：成功，其他失败): 参数：{}",codss);
                                                        //判断新增门店救援记录是否成功
                                                        if (codss==0){
                                                            return new Result(true,StatusCode.ADDED_SUCCESSFULLY,"救援信息记录新增成功");
                                                        }else {
                                                            return new Result(false,StatusCode.ADD_FAILED,"救援信息记录新增失败");
                                                        }
                                                    }else {
                                                        return new Result(false,StatusCode.RESCUEVEHICLE_EMPTY,"输入被救援车型为空");
                                                    }
                                                }else {
                                                    return new Result(false,StatusCode.STORE_SHOPKEEPER_EMPTY,"输入救援车主名为空");
                                                }
                                            }else {
                                                return new Result(false,StatusCode.RESCUETIME_EMPTY,"输入救援时间为空");
                                            }
                                        }else {
                                            return new Result(false,StatusCode.STORE_PHONE_EMPTY,"输入救援门店电话为空");
                                        }
                                    }else {
                                        return new Result(false,StatusCode.STORE_NAME_EMPTY,"输入救援门店名为空");
                                    }
                                }else {
                                    return new Result(false,StatusCode.RESCUEPRICE_EMPTY,"输入救援收费为空");
                                }
                            }else {
                                return new Result(false,StatusCode.PRECISION_EMPTY,"输入救援地址精度为空");
                            }
                        }else {
                            return new Result(false,StatusCode.STORE_PHONE_EMPTY,"输入被救援车主电话为空");
                        }
                    }else {
                        return new Result(false,StatusCode.RESCUELICENSEEMPTY,"输入被救援车主车牌号为空");
                    }
                }else {
                    return new Result(false,StatusCode.LATITUDE_EMPTY,"输入救援地址纬度为空");
                }
            }else {
                return new Result(false,StatusCode.STORE_IMAGE_EMPTY,"输入救援现场图片为空");
            }
        }else {
            return new Result(false,StatusCode.STORE_ADDRESS_EMPTY,"输入救援地址为空");
        }
    }

    /**
     * 功能描述〈根据车牌号查询救援记录〉
     * @author: yanzhongliang
     * @param: rescueLicense
     * @return: list
     * @Date 2021/4/15 17:53
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "根据车牌号查询救援记录",notes = "<span style='color:red;'>用来根据车牌号查询救援记录的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40012,message = "根据车牌号查询救援记录成功",response = RescueDto.class),
            @ApiResponse(code=40011,message = "查询为空"),
            @ApiResponse(code=70043,message = "车牌号为空")
    })
    //@ApiImplicitParams用于描述接口参数
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "rescueLicense",value = "车牌号",dataType = "String",paramType = "query",example = "渝A166411")
    })

    @GetMapping("/RescueLisenten")
    public Result getRescueLicense(@ApiParam String rescueLicense){
        log.info("接收到车牌号：参数：{}",rescueLicense);

        //判断传入参数是否为空
        if (rescueLicense!=null){
            log.info("调用根据车牌号查询救援记录的方法开始");
            List<RescueDto> list=rescueService.getRescueListAndRescueLicense(rescueLicense);

            log.info("调用根据车牌号查询救援记录的方法结束；参数：{}",list);
            //判断查询出来的记录是否为空
            if (list!=null){
                //将查询到的门店详情转成json字符串
                String strJson= JSON.toJSONString(list);
                //存入redis
                redisTemplate.opsForHash().put("Rescue","records",strJson);
                return new Result(true,StatusCode.SEARCH_SUCCESSFUL,"根据车牌号查询救援记录成功",list);

            }else {
                return new Result(false,StatusCode.QUERY_IS_EMPTY,"查询为空");
            }
        }
        return new Result(false,StatusCode.RESCUELICENSEEMPTY,"车牌号为空");
    }

    /**
     * 功能描述〈查询所有道路救援记录〉
     * @author: yanzhongliang
     * @param: null
     * @return: list
     * @Date 2021/4/15 18:54
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "查询所有道路救援记录",notes = "<span style='color:red;'>用来查询所有道路救援记录的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40012,message = "查询所有道路救援记录成功",response = Rescue.class),
            @ApiResponse(code=40011,message = "查询记录为空")
    })

    @GetMapping("/RescueListAll")
    public Result getRescueList() {
            log.info("开始查询所有道路救援记录");
            //调用查询救援记录方法
            List<Rescue> list = rescueService.getRescueList();
            log.info("查询完救援记录：参数：{}", list);
            //判断查询出来的记录是否为空
            if (list!=null) {
                //将查询到的门店详情转成json字符串
                String strJson = JSON.toJSONString(list);
                //存入redis
                redisTemplate.opsForHash().put("Rescue", "recordsAll", strJson);
                return new Result(true, StatusCode.SEARCH_SUCCESSFUL, "查询所有道路救援记录成功", list);
            } else {
                return new Result(false, StatusCode.QUERY_IS_EMPTY, "查询记录为空");
            }
    }
}

